﻿using System.Windows.Media;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Inventory;
using Styx.CommonBot.Routines;
using Styx.WoWInternals.WoWObjects;

namespace WuzzCollection
{
    public partial class AIO : CombatRoutine
    {
        private Classes.AbstractClass Routine = null;

        #region Routine Information
        public sealed override string Name { get { return BotSettings.Name; } }
        public override WoWClass Class { get { return StyxWoW.Me.Class; } }
        public override bool WantButton { get { return true; } }
        public override void OnButtonPress()
        {
            GUI.SettingsForm SForm = new GUI.SettingsForm();
            SForm.ShowDialog();
        }
        #endregion

        public override void Initialize()
        {
            Logging.Write(Colors.LightGreen, "== Thank you for using {0}!", Name);
            switch (StyxWoW.Me.Class)
            {
                case WoWClass.Hunter:
                    Routine = new Classes.Hunter();
                    Logging.Write(Colors.LightGreen, "== Welcome Hunter, let's go for a ride!");
                    break;
                case WoWClass.Monk:
                    Routine = new Classes.Monk();
                    Logging.Write(Colors.LightGreen, "== Welcome Monk, let's go for a ride!");
                    break;
                case WoWClass.Warrior:
                    Routine = new Classes.Warrior();
                    Logging.Write(Colors.LightGreen, "== Welcome Warrior, let's go for a ride!");
                    break;
                default:
                    Logging.Write(Colors.Red, "== {0} is not a supported class!", StyxWoW.Me.Class.ToString());
                    break;
            }
        }

        #region PreCombat Buffs
        public override bool NeedPreCombatBuffs
        {
            get
            {
                return (!StyxWoW.Me.Mounted && Routine.NeedPreCombatBuffs());
            }
        }
        public override void PreCombatBuff()
        {
            if (!StyxWoW.Me.Mounted)
            {
                Routine.PreCombatBuffs();
            }
        }
        #endregion

        #region Rest Behaviour
        public override bool NeedRest { get { return (StyxWoW.Me.HealthPercent <= 65 || (StyxWoW.Me.Auras.ContainsKey("Food") && StyxWoW.Me.HealthPercent < 100)); } }
        public override void Rest()
        {
            // Try to eat if we aren't already
            if (!StyxWoW.Me.Auras.ContainsKey("Food"))
            {
                WoWItem BestFood = Consumable.GetBestFood(false);
                if (BestFood.Use())
                {
                    Logging.Write(Colors.LightGreen, "== Eating " + BestFood.Name);
                }
            }
        }
        #endregion

        public override void Pull()
        {
            if (BotSettings.BotMovement)
            {
                if (StyxWoW.Me.GotTarget && Helpers.Unit.ValidUnit(StyxWoW.Me.CurrentTarget))
                {
                    Routine.Pull();
                }
            }
        }

        public override void Combat()
        {
            using (StyxWoW.Memory.AcquireFrame())
            {
                Helpers.Safers.EnsureTarget();

                if (StyxWoW.Me.GotTarget && Helpers.Unit.ValidUnit(StyxWoW.Me.CurrentTarget) && !StyxWoW.Me.IsCasting)
                {
                    if (BotSettings.BotMovement)
                    {
                        Routine.ApproachAndFaceTarget();
                    }

                    Routine.AutoAttack();

                    // Stop here if we can't cast a spell
                    if (SpellManager.GlobalCooldown) { return; }

                    if (Routine.CastHeal())
                        return;
                    //UseHealthPotion();
                    if (Routine.CastDefensiveSpells())
                        return;
                    if (Routine.CastInterrupt())
                        return;

                    Routine.RunRotation();
                }
            }
        }
    }
}